<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="70" label-position="right">
        <FormItem label="用户名称" prop="user_name">
            <Input type="text" v-model="formValidate.user_name"></Input>
        </FormItem>
        <FormItem label="真实姓名" prop="cn_name">
            <Input type="text" v-model="formValidate.cn_name"></Input>
        </FormItem>
        <FormItem label="密码" prop="password">
            <Input type="text" v-model="formValidate.password"></Input>
        </FormItem>
        <FormItem label="确认密码" prop="password_chk">
            <Input type="text" v-model="formValidate.password_chk"></Input>
        </FormItem>       
        <FormItem label="角色" prop="role_id">
             <Select placeholder="选择角色" v-model="formValidate.role_id">
                <Option :value="1">1</Option> 
                <Option :value="2">2</Option>                     
            </Select>
        </FormItem>       
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">确定</Button>
        </FormItem>
    </Form>
</template>
<script>

export default {
    name: 'managerinfo',
    props: ['formValidate'],
    data(){
        return {            
            salesmans:'',            
            ruleValidate:{
                user_name:[
                    { required: true, message: '请输入用户名称', trigger: 'blur' }
                ],
                cn_name:[
                    { required: true, message: '请输入真实姓名', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入登录密码', trigger: 'blur' }
                ],
                password_chk:[
                    { required: true, message: '请重复输入登录密码', trigger: 'blur' }
                ],
                role_id:[
                    { type:'number', required: true, message: '请选择角色', trigger: 'blur' }
                ]
            }       
        }
    },
    created(){        
        this.$axios({
            mothod: 'post',
            url: 'http://localhost/public/api/manager',
            params: ''
        }).then((response)=> { 
            this.salesmans = response.data;
        })
        .catch(function (error) {
            console.log(error);
        }); 
    },
    methods: { 
        addManager(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/manager/create',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('录入成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('录入失败!');
            });   
        },
        editManager(){
            this.$axios({
                mothod: 'post',
                url: 'http://localhost/public/api/manager/update',
                params: this.formValidate
            }).then((response)=> { 
                this.$Message.success('编辑成功!'); 
            })
            .catch(function (error) {
                this.$Message.error('编辑失败!');
            });   
        },                 
        handleSubmit(name){                 
            this.$refs[name].validate((valid) => {                   
                if (valid) {                                 
                    if(this.formValidate.id != ""){
                        this.editManager(); 
                    }else{
                        this.addManager();                                 
                    }  
                    this.$emit('updateManagerList');    
                    this.$emit('closeDialog');              
                } else {
                    this.$Message.error('提交失败!');
                }
            })
        }
    }
}
</script>
